<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/common.css"/>
    <link rel="stylesheet" href="css/main.css"/>
    <link rel="stylesheet" href="css/jquery-ui.css"/>
    <link rel="stylesheet" href="css/bootstrap.css"/>
    <link rel="stylesheet" href="css/jquery-impromptu.min.css" type="text/css"/>
    <title>借款申请</title>
</head>
<body>
<div class="container">
    <div id="dataLoad" style="display:none; position: fixed; width: 100%; top: 20%; z-index: 1001;">
           <table width=100% height=100% border=0 align=center valign=middle>
            <tr height=50%><td align=center>&nbsp;</td></tr>
            <tr><td align=center><img src="images/loading.gif"/></td></tr>
            <tr><td align=center>提交中，请稍后......</td></tr>
            <tr height=50%><td align=center>&nbsp;</td></tr>
           </table>
         </div>
    <div class="container">
        <div id="forms" class="mt10">
            <div class="box">
                <div class="box_border">
                    <div class="box_top"><b class="pl15">借款申请信息</b></div>
                    <div class="box_center">
                        <form action="" class="">
                            <table class="form_table pt15 pb15" width="100%" border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td class="td_right">所属部门：</td>
                                    <td class="">
                                        <input type="text" placeholder="请输入所属部门" class="input-text lh25" size="20" disabled id="loanMoney_depart">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="td_right">借款人：</td>
                                    <td class="">
                                        <input type="text" placeholder="请输入人员姓名" class="input-text lh25" size="20" disabled id="loanMoney_user">
                                    </td>
                                </tr>

                                <tr>
                                    <td class="td_right">借款事由：</td>
                                    <td class="">
                                        <textarea name="" id="loanMoney_reason" cols="30" rows="10" class="textarea" disabled></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="td_right">借款金额：</td>
                                    <td class="">
                                        <input type="number" placeholder="请输入借款金额" class="input-text lh25" size="20" id="loanMoney_amount" disabled>
                                        <span></span>
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <div class="box_bottom pb5 pt5 pr10 approve_area" style="border:1px solid #dadada;">
            <div class="search_bar_btn" style="text-align:right;">
                <input type="button" id="approve_btn" name="button" class="btn1 btn82 btn_config" value="审批">
            </div>
        </div>
        <div class="box_bottom pb5 pt5 pr10 calc_area" style="border:1px solid #dadada;display:none">
            <div class="search_bar_btn" style="text-align:right;">
                <input type="button" id="check_btn" name="button" class="btn1 btn82 btn_count" value="核算">
            </div>
        </div>
        <div class="main_center span10" id="opinion_area">
            <div class="box_top">
                <b class="pl15">借款审批信息：</b>
            </div>
            <div class="span33 fl pt10">
                <div class="box pr5">
                    <div class="box_border" style="height: 230px">
                        <div class="box_top">
                            <b>部门审批意见</b>
                            <input class="ext_btn ext_btn_submit app_detail" type="submit" value="详情">
                        </div>
                        <div class="box_center opinion" id="section_opinion"><h1>同意借款</h1></div>
                        <div class="box_center sign"><h3>签批人：<img id="section_sign"  width="100px" height="50px"></h3></div>
                    </div>
                </div>
            </div>
            <div class="span33 fl pt10">
                <div class="box pl5 pr5">
                    <div class="box_border" style="height: 230px">
                        <div class="box_top">
                            <b>分管领导意见</b>
                            <input class="ext_btn ext_btn_submit app_detail" type="submit" value="详情">
                        </div>
                        <div class="box_center opinion" id="section_leader_opinion"><h1>同意借款</h1></div>
                        <div class="box_center sign"><h3>签批人：<img id="section_leader_sign"  width="100px" height="50px"></h3></div>
                    </div>
                </div>
            </div>
            <div class="span33 fl pt10">
                <div class="box pl5">
                    <div class="box_border" style="height: 230px">
                        <div class="box_top">
                            <b>局领导意见</b>
                            <input class="ext_btn ext_btn_submit app_detail" type="submit" value="详情">
                        </div>
                        <div class="box_center opinion" id="leader_opinion"><h1>同意借款</h1></div>
                        <div class="box_center sign"><h3>签批人：<img id="leader_sign"  width="100px" height="50px"></h3></div>
                    </div>
                </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div id="app_dialog" class="mt10" style="display:none" title="审批窗口">
        <div class="box">
            <div class="box_center">
                <table class="form_table pb15" width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td class="td_left">审批意见：
                    <span class="">
                        <div class="select_border">
                            <div class="select_containers ">
                                <select id="app_opinion" class="select">
                                    <option value="1">同意</option>
                                    <option value="0">驳回</option>
                                </select>
                            </div>
                        </div>
                    </span>
                        </td>
                    </tr>
                    <tr>
                        <td class="td_left">详细意见：</td>
                    </tr>
                    <tr>
                        <td class="">
                            <textarea name="" id="app_opinion_detail" cols="20" rows="30" placeholder="请填写具体审批意见" class="textarea">同意借款</textarea>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

    <div id="check_dlg" class="mt10" style="display:none" title="借款核销窗口">
        <table class="form_table pt15 pb15" width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr class="fee">
                <td class="td_center">预借费用：</td>
                <td height="30px" style="line-height:30px">
                    <input type="number" name="pre_load_fee" value="0" class="input-text lh25 input_fee" id="check_total_loan_fee" size="10" disabled>
                    <span></span>
                </td>
            </tr>
            <tr class="fee">
                <td class="td_center">已还费用：</td>
                <td height="30px" style="line-height:30px">
                    <input type="number" name="hotel_fee" value="0" class="input-text lh25 input_fee" id="check_return_loan_fee" size="10" disabled>
                    <span></span>
                </td>
            </tr>
            <tr class="fee">
                <td class="td_center">核销费用：</td>
                <td height="30px" style="line-height:30px">
                    <input type="number" placeholder="请输入核销费用" value="0" class="input-text lh25 input_fee" id="check_loan_fee" size="10">
                    <span></span>
                </td>
            </tr>
        </table>
    </div>
</div>
</body>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/colResizable-1.6.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script src="js/config.js" type="text/javascript"></script>
<script src="js/jquery-impromptu.min.js" type="text/javascript"></script>
<script type="text/javascript">
    approveOpinion = {};

    //辅助函数，审批借款
    function approveLoanFee(){
        $('#dataLoad').show();
        $.post(url + 'LoanFee/ApproveOrdinaryLoanFee.php', approveOpinion, function(data){
            $('#dataLoad').hide();
            var t = JSON.parse(data);
            if(t.state == 1){
                $.prompt( "<h3>审批成功</h3>",{
                    title: '消息提醒',
                    buttons: {'确定': true},
                    submit: function(){
                        jumpPage('section_approve.html');
                    }
                });
            }
            else{
                $.prompt( "<h3>网络或数据库错误，请稍后再试</h3>",{
                    title: '消息提醒',
                    buttons: {'确定': true}
                });
            }
        })
    }

    //辅助函数，处理复核和审批框的显示
    function processInfoBtn(){
        var approveState = window.localStorage.getItem('approve_state');

        if(approveState == 0){
            $('.approve_area').hide();
        }
        else{
            if(approveState == 4) {
                $('.calc_area').show();
                $('.approve_area').hide();
            }
            else{
                $('.calc_area').hide();
                $('.approve_area').show();
            }
        }
    }

    //辅助函数，处理基本借款信息审批
    function approveOrdinaryFee(){
        var userInfo = JSON.parse(window.localStorage.getItem('user_info'));
        var identity = window.localStorage.getItem('approve_type');
        var curItem = JSON.parse(window.localStorage.getItem('curItem'));
        approveOpinion.userID = userInfo.user_id;
        approveOpinion.approve_state = identity;
        approveOpinion.approve_id = curItem.data.loan_money_id;
        $('#approve_btn').click(function(){
            $('#app_dialog').dialog({
                resizable: false,
                height: "auto",
                width: 430,
                modal: true,
                title: '借款审批窗口',
                buttons: {
                    '确定' :function(){
                        if($('#app_opinion_detail').val() == ""){
                            $.prompt("<h3>未填写审批意见，请填写</h3>", {
                                title: '错误提醒',
                                buttons: {'确定': true}
                            })
                        }
                        else{
                            approveOpinion.basic_fee_opinion = {
                                'basic_fee_opinion' : $('#app_opinion').val(),
                                'basic_fee_opinion_detail' : $('#app_opinion_detail').val(),
                            };
                            approveLoanFee();
                            console.log(JSON.stringify(approveOpinion));
                            $(this).dialog("close");
                        }
                    },
                    '取消': function(){
                        $(this).dialog("close");
                    }
                }
            });
        })
    }

    //辅助函数,处理借款核销
    function  hexiaoLoanFee(){
        var curItem = JSON.parse(window.localStorage.getItem('curItem'));
        var user_info = JSON.parse(window.localStorage.getItem('user_info'));
        var check_fee = parseFloat($('#check_loan_fee').val()) + parseFloat(curItem.data.return_money);
        if(check_fee > parseFloat(curItem.data.money)){
            $.prompt("<h3>核销借款金额大于实际借款金额，请重新填写</h3>", {
                title: '错误提醒',
                buttons: {'确定': true}
            })
        }
        else{
            $('#dataLoad').show();
            $.post(url + 'LoanFee/FinanceLoanFeeCheck.php',
                    {'loan_fee_id': curItem.data.loan_money_id,
                        'check_fee' : check_fee,
                        'user_id' : user_info.user_id}, function(data){
                        $('#dataLoad').hide();
                        var t = JSON.parse(data);
                        if(t.state == 1){
                            $.prompt("<h3>核销成功</h3>", {
                                title: '提醒',
                                buttons: {'确定': true},
                                submit: function(e, v, m, f){
                                    if(v){
                                        jumpPage('finance_loan_fee_fuhe.html');
                                    }
                                }
                            })
                        }
                        else{
                            $.prompt("<h3>数据库或网络错误，请稍后再试</h3>", {
                                title: '错误提醒',
                                buttons: {'确定': true},
                            })
                        }
                    })
        }
    }

    //辅助函数，显示详细审批意见
    function ShowApproveDetail(curItem){

        $('.app_detail').each(function(){
            $(this).unbind('click');
        });
        var app_details = $('.app_detail');
        $(app_details[0]).click(function(){
            $.prompt( "<p>" + (curItem.data.section_approve_opinion == '' ? '未审批' : curItem.data.section_approve_opinion) + "</p>",{
                title: '审批详细信息',
                buttons: {'确定': true}
            });
        })
        $(app_details[1]).click(function(){
            $.prompt( "<p>" + (curItem.data.section_leader_opinion == '' ? '未审批' : curItem.data.section_leader_opinion) + "</p>",{
                title: '审批详细信息',
                buttons: {'确定': true}
            });
        })
        $(app_details[2]).click(function(){
            $.prompt( "<p>" + (curItem.data.leader_opionion == '' ? '未审批' : curItem.data.leader_opionion) + "</p>",{
                title: '审批详细信息',
                buttons: {'确定': true}
            });
        })

    }

    //初始化函数借款数据
    function initLoanFeeData(curItem){
        $('#loanMoney_depart').val(curItem.data.section_name);
        $('#loanMoney_user').val(curItem.data.user_name);
        $('#loanMoney_reason').val(curItem.data.reason);
        $('#loanMoney_amount').val(curItem.data.money);

        $('#check_total_loan_fee').val(curItem.data.money);
        $('#check_return_loan_fee').val(curItem.data.return_money);
        $('#check_loan_fee').val(curItem.data.money - curItem.data.return_money);

    }
    $(function(){
        var curItem = JSON.parse(window.localStorage.getItem('curItem'));

        initLoanFeeData(curItem);
        setApproveOpinion(curItem.data, 2);
        processInfoBtn();
        //审批按钮
        approveOrdinaryFee();
        ShowApproveDetail(curItem);

        $('#check_btn').click(function(){
            $('#check_dlg').dialog({
                resizable: false,
                height: "auto",
                width: "auto",
                modal: true,
                buttons:{
                    '确定' :function() {
                        $('#check_dlg').dialog('close');
                        hexiaoLoanFee();
                    },
                    '取消' : function(){
                        $('#check_dlg').dialog('close');
                    }
                }
            })
        });


        $('#app_opinion').change(function(){
            if($(this).val() == 1){
                $('#app_opinion_detail').val('同意借款');
            }
            else{
                $('#app_opinion_detail').val('');

            }
        })
    })

</script>